# Figure 2 
# Generate 0.9-Lorenz curves for identical, independent, comonotonic and
# countermonotonic attributes (also intermediate association)
set.seed(102112516) 
N <- 15000 # sample size
M <- N     # sample size of pseudo sample

rho <- c(-0.99,-0.6,-0.3,0,0.3,0.6,0.99) # degree of associations to use
# generate bivariate lognormal
Lognormal <- lapply(rho,function(p){ 
  X <- mvrnorm(N,c(0,0),matrix(c(1,p,p,1),nrow=2))
  return(exp(X))
})
# calculate the ILFS
ilf_lnorm <- lapply(Lognormal, function(list){ 
  ILF(list,rep(1/N,N),vquantile(list),M)
})

ilfs <- ilf_lnorm

# Generate a grid
sequence <- rep(seq(from = 0, to = 1, by = 1/(nrow(ilfs[[1]]$estimate) - 1)))
s <- 0
t <- 0
x <- rep(0,length(sequence)^2)
y <- x
for (i in 1:length(sequence)){
  s <- s+1
  k <- length(sequence)*(s - 1) + 1
  j <- length(sequence)*s
  x[k:j] <- rep(sequence[s],length(sequence))
  y[k:j] <- sequence
  
}

# Calculate the coordinates of the corners of the identical distribution
# There are more alphas than necessary, we only use 0.9 in the figure.
alpha <- c(0.1,0.25,0.3,0.4,0.5,0.6,0.7,0.75,0.8,0.9,0.95,0.99)
egal <- rep(0,12)
for (k in 1:12){
  # This formula is given in paper section 1.2.2 example 1, set d = 2
  egal[k] <- uniroot(function(x){x*(1-log(x)) - alpha[k]},c(0.001,1))$root
}

lr <- data.frame(x,y,sapply(1:7,function(i){c(ilfs[[i]]$estimate)}))

levels <- c(0.9)
sz <- 1
sz2 <- 1.2

lcontours <- ggplot(lr,aes(x=x,y=y)) + 
  annotate("segment",x = round(egal[10],3), y = round(egal[10],3), xend = egal[10], yend = 1,linetype = 3, size=sz2)+
  annotate("segment",x = round(egal[10],3), y = round(egal[10],3), xend = 1, yend = egal[10],linetype = 3, size = sz2)+
  geom_contour(aes(z=X1),color ="black",breaks = levels, size =sz2 ) +
  geom_contour(aes(z=X2),color ="darkgray",breaks = levels,size =sz) +
  geom_contour(aes(z=X3),color ="darkgray",breaks = levels,size =sz) +
  geom_contour(aes(z=X4),color ="black",breaks = levels,size =sz2 ) +
  geom_contour(aes(z=X5),color ="darkgray",breaks = levels,size =sz) +
  geom_contour(aes(z=X6),color ="darkgray",breaks = levels,size =sz) +
  geom_contour(aes(z=X7),color ="black",breaks = levels,size =sz) +
  annotate("label", x=0.7, y=0.33, label= "Independent", family = 'serif',color ="black",fill = "white", size = 5.5) +
  annotate("label", x=0.7, y=0.5, label= "Countermonotonic", family = 'serif',color ="black",fill = "white", size = 5.5) +
  annotate("label", x=0.7, y=0.265, label= "Comonotonic", family = 'serif',color ="black",fill = "white", size = 5.5) + 
  annotate("label", x=0.7, y=egal[10], label= "Identical", family = 'serif', color ="black",fill = "white", size = 5.5) +
  scale_color_manual(values=c("black")) +
  coord_cartesian(xlim = c(0.25, 0.8),ylim = c(0.25, 0.8)) +
  theme(plot.title = element_text(hjust = 0.5)) + xlab("")+ ylab("")+
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.6),
        panel.background = element_rect(fill = 'transparent'),
        panel.grid = element_blank(),
        legend.position="none",
        legend.key = element_rect(fill = 'transparent'),
        text = element_text(family = "serif",size=22))+
  scale_y_continuous(breaks = seq(0.2,1,0.1),position = "right",labels = seq(0.2,1,0.1)) +
  scale_x_continuous(breaks = seq(0.2,1,0.1),position = "top",labels = seq(0.2,1,0.1))
ggsave(plot = lcontours, width = 6, height = 6, filename = "fig2.png")



